function toKeyValPair(names, values)
{
	var result = {};
	for(var i = 0; i < names.length; i++)
	{
		result[names[i]] = values[i];
	}
	return result;
}

function decileTable()
{
	var tableObjectArray = [];
	$.ajax(
	{
		url: '/misc/weber/CSEvals/ranking.cfm',
		type: "GET",
		dataType: "json",
		async: false,
		success:function(data)
		{
			var dataArray;
			var tableObject;
			$.each(data.DATA, function(i, array)
			{
				dataArray = toKeyValPair(data.COLUMNS, array);	//CONVERTS DATA TO A KEY VALUE PAIR FOR READABILITY
				tableObject = {studentCount:dataArray["STUDENTCOUNT"], percentile:dataArray["DECTILE"], course:dataArray["COURSE"], instructor:dataArray["INAME"], score:dataArray["INSTRUCTORAVERAGE"]};
				tableObjectArray.push(tableObject);
			});
		}
	});
	//TEMPORARY DATA FOR TESTING
	// var tableObject = {percentile:9, course:"3750", instructor:"Peterson, Brad", score:3.64};
	// var tableObject2 = {percentile:7, course:"1010", instructor:"Hilton, Spencer", score:3.26};
	// var tableObject3 = {percentile:5, course:"1410", instructor:"Jensen, Joshua", score:3.05};
	// var tableObject4 = {percentile:5, course:"2350", instructor:"Jensen, Joshua", score:3.02};
	// var tableObject5 = {percentile:2, course:"3740", instructor:"Peterson, Brad", score:2.63};
	// var tableObject6 = {percentile:2, course:"4750", instructor:"Jensen, Joshua", score:2.59};
	// var tableObject7 = {percentile:1, course:"3250", instructor:"Hilton, Spencer", score:2.34};
	// var tableObject8 = {percentile:1, course:"3750", instructor:"Hilton, Spencer", score:2.24};
	// var tableObject9 = {percentile:1, course:"1400", instructor:"Peterson, Brad", score:2.01};
	
	// var tableObjectArray = [tableObject, tableObject2, tableObject3, tableObject4, tableObject5, tableObject6, tableObject7, tableObject8, tableObject9];
	//tableObjectArray[0] = tableObject;
	
	
	var rankTable =	'<table>' + 
		
		//TOP ROW LABELS
		'<tr><th>Percentile</th><th>Course</th><th>Instructor</th><th>Score</th></tr>';
		
		for (var row = 10; row > 0; row = row - 1)
		{
			//LEFT COLUMN: PERCENTILES
			
			rankTable+='<tr class="innerRowBorder"><td>';
			if (row == 10)
			{
				rankTable += '>90';
			}
			else if (row == 1)
			{
				rankTable += '<10';
			}
			else
			{
				rankTable += (row - 1) + '0 to ' + (row - 1) + '9';
			}
			
			rankTable+='</td>';
			
			//RIGHT COLUMNS: COURSE, INSTRUCTOR, SCORE
			var tempCount = 0;
			for (var i = 0; i < tableObjectArray.length; i++)
			{
				if (tableObjectArray[i]["percentile"] > (row - 1) && tableObjectArray[i]["percentile"] <= (row))
				{
					rankTable += '<td>' + tableObjectArray[i]["course"] + '</td>';
					rankTable += '<td>' + tableObjectArray[i]["instructor"] + '</td>';
					rankTable += '<td>' + tableObjectArray[i]["score"] + '</td>';
					rankTable += '</tr><tr><td></td>';
					tempCount++;
				}
			}
			//rankTable += '<td class = "linerow">line</td>';
			if (tempCount == 0)
			{
				rankTable+= '<td>-</td><td>-</td><td>-</td></tr>';
			}
			else
			{
				rankTable += '<td></td><td></td><td></td></tr>';
			}

		}

	rankTable += '</table>';
	    
	document.write(rankTable);

}

/*window.onload=(function() 
{
	decileTable();
});*/